package reports;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fj.F;

import server.db.DatabaseFunctor;
import server.db.StmtHelper;
import server.objects.Photo;

import ajaxmethods.SessionMethod;

public class MapReport extends SessionMethod {
	Map<String,String> listingMap = new HashMap<String,String>();
	private F<List<Map<String, String>>, DatabaseFunctor<List<String>>> getPhotos = new F<List<Map<String, String>>, DatabaseFunctor<List<String>>>() {

		@Override
		public DatabaseFunctor<List<String>> f(List<Map<String, String>> arg0) {
			listingMap = arg0.get(0);
			return StmtHelper.db("select image_url from photos where zillowid = ").addVariable(listingMap.get("zillowID")).collectStrings("image_url");
		}
		
	};
	
	private static String selectList = "zillowID,useCode,lotsize,finishedSQFT,bathrooms,bedrooms,zip,city,state,street,price,ifnull((select image_url from photos where photos.zillowID=al.zillowID and photos.photo_num=1),'"+Photo.getNoPhotoURL()+"')image_url";
	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		try {
			super.perform(request, response);
			response.setContentType("text/json");
			List<String> photosArray = StmtHelper.db("select "+selectList+" from alllist al where " +
					" al.zillowID=")
			.addVariable(request.getParameter("zillowID")).collectIntoMaps.provideTo(getPhotos).unsafeGet();
			StringBuffer sb = new StringBuffer();
			
			sb.append("<table style=\"width: 260px;height:150px;\" class=\"mapContentWrapper\"><tr ><td colspan='2'><div>"+listingMap.get("street")+"</div></td></tr>");
			sb.append("<tr><td><div class=\"secondaryFields\"><div style=\"white-space:nowrap;\">"+listingMap.get("city")+", "+listingMap.get("state")+"</div>");
			sb.append("<div><span>Beds:</span> "+listingMap.get("bedrooms")+" <span>Baths:</span> "+listingMap.get("bathrooms")+"</div>");
			sb.append("<div><span>SqFt:</span> "+listingMap.get("finishedSQFT")+"</div>");
			String price = "";
			if(listingMap.get("price")==null || listingMap.get("price").equals(""))
				price = "N/A";
			else
				price = NumberFormat.getCurrencyInstance().format(Integer.parseInt(listingMap.get("price")));
			sb.append("<div><span>Price:</span> "+price+"</div></div>");
			sb.append("<div id=\"detailview\"><a  id=\""+request.getParameter("zillowID")+"\" title=\""+listingMap.get("street")+" "+listingMap.get("city")+", "+listingMap.get("state")+"\" class=\"detailLink moredetails\">More Details</a></div></td>");
			sb.append("<td><div style=\"float:left;padding-left:2px;\" ><a ");
			if(photosArray!=null && photosArray.size()>0)
				sb.append("onclick=\"doFancy(this)\"");
			sb.append(" value=\""+listingMap.get("image_url")+"\"><img style=\"width:115px;\" src=\""+listingMap.get("image_url")+"\"></a></div>");
			sb.append("</td></tr></table>");
			StringBuffer scriptBuffer = new StringBuffer();
			scriptBuffer.append("<script>");
			scriptBuffer.append("function doFancy(elem) {" +
					"$.fancybox([");
			for(int i=0;i<photosArray.size();i++)
			{
				if(i+1<photosArray.size())
					scriptBuffer.append("'"+photosArray.get(i)+"',");
				else
					scriptBuffer.append("'"+photosArray.get(i)+"'");
			}
			scriptBuffer.append("],{type:'image',title:'"+listingMap.get("street")+"', transitionIn:'elastic',transitionOut:'elastic',easingIn:'easeOutBack',easingOut:'easeInBack'});" +
					"}");
			scriptBuffer.append(DetailReport.javaScriptOut());
			scriptBuffer.append("</script>");
			data.put("listingMapPopup", sb.toString()).put("runScript", scriptBuffer).write(response.getWriter());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
